{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f6eb55927f50a5ec",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "# 1 Numpy介绍\n",
    "- Numpy（Numerical Python）是一个开源的Python科学计算库，用于快速处理任意维度的数组。\n",
    "- Numpy支持常见的数组和矩阵操作。对于同样的数值计算任务，使用Numpy比直接使用Python要简洁的多。\n",
    "- Numpy使用ndarray对象来处理多维数组，该对象是一个快速而灵活的大数据容器。\n",
    "# 2 ndarray介绍\n",
    "NumPy提供了一个N维数组类型ndarray，它描述了相同类型的“items”的集合。\n",
    "用ndarray进行存储："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8a9653d1fc189cd7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-20T07:58:24.661284300Z",
     "start_time": "2024-02-20T07:58:24.642169100Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[80, 89, 86, 67, 79],\n",
       "       [78, 97, 89, 67, 81],\n",
       "       [90, 94, 78, 67, 74],\n",
       "       [91, 91, 90, 67, 69],\n",
       "       [76, 87, 75, 67, 86],\n",
       "       [70, 79, 84, 67, 84],\n",
       "       [94, 92, 93, 67, 64],\n",
       "       [86, 85, 83, 67, 80]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 创建ndarray\n",
    "score = np.array(\n",
    "[[80, 89, 86, 67, 79],\n",
    "[78, 97, 89, 67, 81],\n",
    "[90, 94, 78, 67, 74],\n",
    "[91, 91, 90, 67, 69],\n",
    "[76, 87, 75, 67, 86],\n",
    "[70, 79, 84, 67, 84],\n",
    "[94, 92, 93, 67, 64],\n",
    "[86, 85, 83, 67, 80]])\n",
    "\n",
    "score"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65877f49e09df380",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "# 3 效率对比\n",
    "**ndarray与Python原生list运算效率对比**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "56a87325d9f18ac0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-20T07:58:35.906016500Z",
     "start_time": "2024-02-20T07:58:24.665284100Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 0 ns\n",
      "Wall time: 320 ms\n",
      "CPU times: total: 15.6 ms\n",
      "Wall time: 63 ms\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "import numpy as np\n",
    "a = []\n",
    "for i in range(100000000):\n",
    "    a.append(random.random())\n",
    "\n",
    "# 通过%time魔法方法, 查看当前行的代码运行一次所花费的时间\n",
    "%time sum1=sum(a)\n",
    "\n",
    "b=np.array(a)\n",
    "\n",
    "%time sum2=np.sum(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73acddf73bbb010f",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "> Numpy专门针对ndarray的操作和运算进行了设计，所以数组的存储效率和输入输出性能远优于Python中的嵌套列表，数组越大，Numpy的优势就越明显。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9285acea5dd040ef",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "# 4 ndarray的优势\n",
    "## 4.1 内存块风格\n",
    "ndarray到底跟原生python列表内存块区别\n",
    "![](../.images/numpy内存地址.png)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c950c6d4763148b",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "> - ndarray在存储数据的时候，数据与数据的地址都是连续的，这样就给使得批量操作数组元素时速度更快。\n",
    "> - ndarray中的所有元素的类型都是相同的，而Python列表中的元素类型是任意的，所以ndarray在存储元素时内存可以连续，而python原生list就只能通过寻址方式找到下一个元素\n",
    "> - 这虽然也导致了在通用性能方面Numpy的ndarray不及Python原生list，但在科学计算中，Numpy的ndarray就可以省掉很多循环语句，代码使用方面比Python原生list简单的多。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7ce383f5c104c32",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "## 4.2 支持并行化运算\n",
    "numpy内置了并行运算功能，当系统有多个核心时，做某种计算时，numpy会自动做并行计算（向量化运算）。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57e2be01a27660f4",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "## 4.3 效率远高于纯Python代码\n",
    "Numpy底层使用C语言编写，内部解除了GIL（全局解释器锁），其对数组的操作速度不受Python解释器的限制，所以，其效率远高于纯Python代码。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc87124040276fa7",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "# 5 小结\n",
    "- numpy介绍【了解】\n",
    "    - 一个开源的Python科学计算库\n",
    "    - 计算起来要比python简洁高效\n",
    "    - Numpy使用ndarray对象来处理多维数组\n",
    "- ndarray介绍【了解】\n",
    "    - NumPy提供了一个N维数组类型ndarray，它描述了相同类型的“items”的集合。\n",
    "    - 生成numpy对象:np.array()\n",
    "- ndarray的优势【掌握】\n",
    "    - 内存块风格\n",
    "        - list -- 分离式存储,存储内容多样化\n",
    "        - ndarray -- 一体式存储,存储类型必须一样\n",
    "    - ndarray支持并行化运算（向量化运算）\n",
    "    - ndarray底层是用C语言写的,效率更高,释放了GIL"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
